/**
 * 项目名称: Topic_437.cpp
 * 项目路径: study/DataStructure/LeetCode/树/中等
 * 编辑时间: 2024年08月19日08时31分22秒
 * 用   户: 轻尘
 */

#include <bits/stdc++.h>

using namespace std;

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;

    TreeNode() : val(0), left(nullptr), right(nullptr) {}

    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}

    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};

class Solution {
public:
    vector<TreeNode*>vn;
    int ans;
    void pre(TreeNode* root){
        if(root){
            vn.push_back(root);
            pre(root->left);
            pre(root->right);
        }
    }
    void path(TreeNode* root,int targetSum,long currTal){
        if(root){
            currTal+=root->val;
            if(currTal == targetSum)ans++;
            path(root->left,targetSum,currTal);
            path(root->right,targetSum,currTal);
        }
    }
    int pathSum(TreeNode* root, int targetSum) {
        if(!root)return 0;
        pre(root);

        for(auto x:vn)path(x,targetSum,0);
        return ans;
    }
};
